package daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.AppDao;
import pojo.MallHomeImage;
import utils.DruidUtils;

public class AppDaoImpl implements AppDao{

	
	
	@Override
	public boolean addUserMoney(int userid, int totalMoney) {
		Connection conn = DruidUtils.getConnection();
		PreparedStatement prep =null;
		try {
			String sql = "update user set money=? where userid=?";
			prep = conn.prepareStatement(sql);
			prep.setInt(1, totalMoney);
			prep.setInt(2, userid);
			int result = prep.executeUpdate();
			if (result > 0) {
				return true;
			}
			return false;

		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if( prep !=null)   prep.close();
				if (conn != null)  conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	@Override
	public int getUserid(String token) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from loginsession where token=?;";
		int userid=0;
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setString(1, token);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				
				userid = res.getInt("userid");
				
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userid;
	}

	@Override
	public int getUserMoney(int userid) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select money from user where userid=?;";
		int userMoney=0;
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setInt(1, userid);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				
				userMoney = res.getInt("money");
				
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userMoney;
	}
	

}
